package com.wisdudu.blank.data.repo;

import com.orhanobut.hawk.Hawk;
import com.wisdudu.blank.support.util.NetUtil;

/**
 * Created by qyf on 2016/9/30.
 * 数据过期与否，常量及时间设置
 * 简单起见，未考虑缓存更新的问题
 */

public class DataStaleConstant {
    public static final int STALE_MS = 30 * 1000;//通用数据有效时间

    public static final int DEVICE_MANAGE_DETAIL_STALE_MS = 5 * 1000;//设备详情有效时间
    public static final int DAY_STALE_MS = 24 * 60 * 60 * 1000;//持续一天
    public static final int PRODUCT_MS = 3600 * 72 * 1000;//获取产品列表有效时间

    public static final String GET_FAMILY_MEMBER_LIST = "getFamilyMemberList";//该方法数据过期时间
    public static final String GET_MODE_LIST = "getModeList";//情景模式过期时间
    public static final String GET_ROOM_LIST = "getRoomList";//家庭房间过期时间
    public static final String GET_DEVICE_MANAGE_LIST = "getDeviceManageList";//该方法数据过期时间
    public static final String GET_CONTROL_DEVICE = "getControlDevice";//该方法数据过期时间
    public static final String GET_DEVICE_MANAGE_DETAIL_BY_EQMID = "getDeviceManageDetailByEqmid";//该方法数据过期时间
    public static final String GET_DEVICE_CATE = "getDeviceCate";
    public static final String GET_DEVICE_BY_ID = "getDevicesById";
    public static final String GET_BRAND_LIST = "getBrandList";
    public static final String GET_PRODUCT_LIST = "getProductList";
    public static final String GET_USER_COUNT = "getUserCount";
    public static final String GET_CITY_INFO = "getCityInfo";
    public static final String GET_HOME_INFO = "getHomeInfo";
    public static final String GET_SYSTEM_IMG = "getSystemImg";

//    public static final String GET_FAMILY_MEMBER_LIST_CACHE = "getFamilyMemberList";//该方法数据过期时间


    /**
     * 数据是否有效,安全
     *
     * @param stalekey stale_key
     * @return 缓存有效：true
     */
    public static boolean isUsable(String stalekey) {
        if (!NetUtil.INSTANCE.isConnected()) {
            return true;
        }
        Long cacheTime = Hawk.get(stalekey, 0L);
        switch (stalekey) {
            case GET_DEVICE_MANAGE_DETAIL_BY_EQMID:
                return !(System.currentTimeMillis() - cacheTime > DEVICE_MANAGE_DETAIL_STALE_MS);
            case GET_BRAND_LIST:
                return !(System.currentTimeMillis() - cacheTime > DAY_STALE_MS);
//            case GET_PRODUCT_LIST:
            case GET_CITY_INFO:
            case GET_SYSTEM_IMG:
                return !(System.currentTimeMillis() - cacheTime > PRODUCT_MS);
            default:
                return !(System.currentTimeMillis() - cacheTime > STALE_MS);
        }
    }

    /**
     * 更新缓存时间
     *
     * @param stalekey
     */
    public static void updateCacheTime(String stalekey) {
        Hawk.put(stalekey, System.currentTimeMillis());
    }

    public static void updateCacheTime(String stalekey, long time) {
        Hawk.put(stalekey, time);
    }

    /**
     * 强制过期
     *
     * @param stalekey
     */
    public static void forceStale(String stalekey) {
        Hawk.put(stalekey, 0L);
    }

//    public static void isCacheUpdate(){
//        Hawk.
//    }
//    public static void setCacheUpdate(){
//        Hawk.put(GET_FAMILY_MEMBER_LIST_CACHE,)
//    }
}
